/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Model.CategProd;
import Model.Promotions;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Anthares
 */
public class PromotionManager extends BaseManager {

    public PromotionManager(SessionFactory factory, String locale) {
        super(factory, locale);
    }
    
    public Promotions getCurrentBestPromotionByProduct(int prodId){
        List<Promotions> proms = this.getCurrentPromotionsByProduct(prodId);
        double minPerc = Double.MAX_VALUE,temp;
        Promotions prom=null;
        for(Promotions promotions : proms){
            if((temp=promotions.getPourcentage())<minPerc){
                minPerc=temp;
                prom=promotions;
            }
        }
        return prom;
    }
    
    public List<Promotions> getCurrentPromotionsByProduct(int prodId){
        Session sess = factory.openSession();
        List<Promotions> proms = (List<Promotions>)sess
                .createSQLQuery("select * from promotions prom, promo_prod pp "+
                "where pp.prod_id=:prodId "+
                "and pp.promo_id = prom.promo_id "+
                "and prom.date_deb <= :curDate "+
                "and prom.date_fin > :curDate")
                .addEntity("prom",Promotions.class)
                .setDate("curDate", new Date()).setInteger("prodId", prodId).list();
        sess.close();
        return proms;
    }
    public Promotions getCurrentBestPromotionByCateg(int categId){
        List<Promotions> proms = this.getCurrentPromotionsByCateg(categId);
        double minPerc = Double.MAX_VALUE,temp;
        Promotions prom=null;
        for(Promotions promotions : proms){
            if((temp=promotions.getPourcentage())<minPerc){
                minPerc=temp;
                prom=promotions;
            }
        }
        return prom;
    }
    
    public List<Promotions> getCurrentPromotionsByCateg(int categID){
        Session sess = factory.openSession();
        List<Promotions> proms = (List<Promotions>)sess
                .createSQLQuery("select * from promotions prom, promo_categ pp "+
                "where pp.categ_id=:categId "+
                "and pp.promo_id = prom.promo_id "+
                "and prom.date_deb <= :curDate "+
                "and prom.date_fin > :curDate")
                .addEntity("prom",Promotions.class)
                .setDate("curDate", new Date()).setInteger("categId", categID).list();
        sess.close();
        return proms;
    }
    public List<Promotions> getCurrentPromotions(){
        Session sess = factory.openSession();
        List<Promotions> proms = (List<Promotions>)sess
                .createSQLQuery("select * from promotions prom "+
                "where prom.date_deb <= :curDate "+
                "and prom.date_fin > :curDate")
                .addEntity("prom",Promotions.class)
                .setDate("curDate", new Date()).list();
        sess.close();
        return proms;
    }
}
